package com.liao.badminton.handler.security;

import com.liao.badminton.service.PermissionApi;
import com.liao.badminton.code.util.SecurityFrameworkUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;

import static com.liao.badminton.code.util.SecurityFrameworkUtils.getLoginUserName;


/**
 * 默认的 {@link SecurityFrameworkService} 实现类
 *
 * @author 芋道源码
 */
@AllArgsConstructor
public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {

    private final PermissionApi permissionApi;

    @Override
    public boolean hasPermission(String permission) {
        return hasAnyPermissions(permission);
    }

    @Override
    public boolean hasAnyPermissions(String... permissions) {
        return permissionApi.hasAnyPermissions(getLoginUserName(), permissions);
    }

    @Override
    public boolean hasRole(String role) {
        return hasAnyRoles(role);
    }

    @Override
    public boolean hasAnyRoles(String... roles) {
        return permissionApi.hasAnyRoles(getLoginUserName(), roles);
    }

    @Override
    public boolean hasScope(String scope) {
        return hasAnyScopes(scope);
    }

    @Override
    public boolean hasAnyScopes(String... scope) {
        UserDetails user = SecurityFrameworkUtils.getLoginUser();
        if (user == null) {
            return false;
        }
        return true;
    }

}
